package cn.cainiao.dao.impl;

import cn.cainiao.dao.PAckageDao;
import cn.cainiao.entity.Packages;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PackageDaoImpl implements PAckageDao {
    /**
     * 显示所有包裹信息
     * @return
     */
    @Override
    public List<Packages> allPackage() {
        Connection conn = null;
        PreparedStatement pat = null;
        ResultSet rs = null;
        Packages packages = null;
        List<Packages> c = new ArrayList<Packages>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cainiaoyizhan", "root", "root");
            pat = conn.prepareStatement("select  id,logistics,ood,consignee,phone,puttime,state,goodsshelf,note,signfortime,warehousing,users,remark,number  from expressage");
            rs = pat.executeQuery();
            while (rs.next()){
                packages = new Packages();
                packages.setId(rs.getInt("id"));
                packages.setLogistics(rs.getString("logistics"));
                packages.setOod(rs.getString("ood"));
                packages.setConsignee(rs.getString("consignee"));
                packages.setPhone(rs.getString("phone"));
                packages.setPuttime(rs.getString("puttime"));
                packages.setState(rs.getString("state"));
                packages.setGoodsshelf(rs.getString("goodsshelf"));
                packages.setNote(rs.getString("note"));
                packages.setSignfortime(rs.getString("signfortime"));
                packages.setWarehousing(rs.getString("warehousing"));
                packages.setNumber(rs.getString("number"));
                packages.setUsers(rs.getString("users"));
                packages.setRemark(rs.getString("remark"));
                c.add(packages);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return c;
    }

    /**
     * 增加包裹信息
     * @param packages
     * @return
     */
    @Override
    public int Addpackage(Packages packages) {
        Connection conn = null;
        PreparedStatement pat = null;
        int count = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cainiaoyizhan", "root", "root");
            pat = conn.prepareStatement("insert into expressage values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            pat.setString(1,"id");
            pat.setString(2,"logistics");
            pat.setString(3,"ood");
            pat.setString(4,"consignee");
            pat.setString(5,"phone");
            pat.setString(6,"puttime");
            pat.setString(7,"state");
            pat.setString(8,"goodsshelf");
            pat.setString(9,"note");
            pat.setString(10,"signfortime");
            pat.setString(11,"warehousing");
            pat.setString(12,"number");
            pat.setString(13,"users");
            pat.setString(14,"remark");
            count = pat.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据快递单号删除快递信息
     * @param ood
     * @return
     */
    @Override
    public int delectpackage(String ood) {
        Connection conn = null;
        PreparedStatement pat = null;
        int count = -1;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cainiaoyizhan", "root", "root");
            pat = conn.prepareStatement("delete from expressage where ood = ?");
            pat.setString(1,ood);
            count = pat.executeUpdate();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 快递基本信息修改
     * @param packages
     * @return
     */
    @Override
    public int updatePackages(Packages packages) {
        return 0;
    }

    /**
     * 根据收货人姓名查找快递
     * @param consignee
     * @return
     */
    @Override
    public List<Packages>qieryPackageBy(String consignee) {
        Connection conn = null;
        PreparedStatement pat = null;
        ResultSet rs = null;
        Packages packages = null;
        List<Packages> c = new ArrayList<Packages>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cainiaoyizhan", "root", "root");
            pat = conn.prepareStatement("select id,logistics,ood,consignee,phone,puttime,state,goodsshelf,note,signfortime,warehousing,users,remark,number from expressage where consignee = ?");
            pat.setString(1,consignee);
            rs = pat.executeQuery();
            while (rs.next()){
                packages = new Packages();
                packages.setId(rs.getInt("id"));
                packages.setLogistics(rs.getString("logistics"));
                packages.setOod(rs.getString("odd"));
                packages.setConsignee(rs.getString("consignee"));
                packages.setPhone(rs.getString("phone"));
                packages.setPuttime(rs.getString("puttime"));
                packages.setState(rs.getString("state"));
                packages.setGoodsshelf(rs.getString("goodsshelf"));
                packages.setNote(rs.getString("note"));
                packages.setSignfortime(rs.getString("signfortime"));
                packages.setWarehousing(rs.getString("warehousing"));
                packages.setNumber(rs.getString("number"));
                packages.setUsers(rs.getString("users"));
                packages.setRemark(rs.getString("remark"));
                c.add(packages);
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return c;
    }
    }
